<?php

class Admin_status extends Datagrid_Controller {

    public function __construct() {
        parent::__construct();

        $this->load->model('Status_m');
        $this->_data['sidebar'] = 'status';

        $this->datagrid->id_key_name = 'status_id';
        $this->datagrid->table_name = 'status';
        $this->datagrid->select_keys = array('status.status_id', 'name', 'color','delete');
        $this->datagrid->display_keys = array('status_id', 'name', 'color');
        $this->datagrid->column_type = array(DG_ID, DG_TEXT, DG_COLOR);
        $this->datagrid->column_editable = array(0, 1, 1);

        $this->datagrid->joins = array(
            array('status_lang', 'status.status_id = status_lang.status_id')
        );
        $this->datagrid->where = array('lang_id' => $this->_data['lang']);
        
        $this->datagrid->update_cfg[] = array(
            'update_where' => 'lang_id = ' . $this->_data['lang'],
            'update_key_name' => 'status_id',
            'update_table' => 'status_lang'
        );        
        
        $this->datagrid->column_config[1]['update_cfg_id'] = 0;
        
        $this->datagrid->row_delete_cfg[] = array(
            'delete_key_name' => 'status_id',
            'delete_table' => 'status_lang'
        );
    }

    function index() {
        $this->datagrid->reset_column_setting();

        // init searchable
        $this->datagrid->columns[1]['bSearchable'] = true;

        $this->datagrid->th_label = array('<input type="checkbox" id="check_header"/>', lang('status_name'), lang('color'));
        $this->datagrid->th_width = array('75px', FALSE, '100px');
        $this->datagrid->div_id = 'status_data';

        $this->datagrid->dg_heading = lang('list') . ' ' . lang('status');
        $this->datagrid->render();
    }

    function _format($key, $row, $column_index, $pos=0) {
         if ($this->datagrid->column_type[$column_index]==DG_ID) {            
            $ret = '<div id="action"><input class="action_chk" type="checkbox" name="cid" value="' . $row['status_id'] . '">';            
            $ret .= '<a class="action_edit" href="' . site_url('admin_status/edit') . '/' . $row['status_id'] . '">&nbsp;</a>';            
            if ($row['delete']!=0) {
                $ret .= '<a class="action_del" onclick = "return confirm(\'Are you sure?\')" href="' . site_url('admin_status/delete') . '/' . $row['status_id'] . '">&nbsp;</a>';
            }
            $ret .= '</div>';
            return $ret;   
         }
        return $this->datagrid->format($key, $row, $column_index, $pos);
    }     
    
    function add_status() {
        is_admin();
        $this->session->set_userdata('current_url', current_url());
        if ($this->input->post('add')) {
            $formVal = array(
                array(
                    'field' => 'name2',
                    'label' => lang('status_name') . ' ' . lang('vi'),
                    'rules' => 'required'
                ),
                array(
                    'field' => 'name1',
                    'label' => lang('status_name') . ' ' . lang('en'),
                    'rules' => 'required'
                )
            );
            $this->form_validation->set_rules($formVal);
            if ($this->form_validation->run() == TRUE) {
                if ($this->Status_m->add_status() > 0) {
                    $this->session->set_flashdata('error', lang('add') . ' ' . lang('success'));
                } else {
                    $this->session->set_flashdata('error', lang('add') . ' ' . lang('fail'));
                }
                redirect('admin_status');
            } else {
                $this->display_admin('admin/status/add_status');
            }
        } else {
            $this->display_admin('admin/status/add_status');
        }
    }

    function edit($id) {
        is_admin();
        $this->session->set_userdata('current_url', current_url());
        $this->_data['detail_status'] = $this->Status_m->get_status_by_id($id, 1);
        $this->_data['detail_status_vi'] = $this->Status_m->get_status_by_id($id, 2);
        if ($this->input->post('submit')) {
            $formVal = array(
                array(
                    'field' => 'name2',
                    'Label' => lang('status_name') . ' ' . lang('vi'),
                    'rules' => 'required'
                ),
                array(
                    'field' => 'name1',
                    'Label' => lang('status_name') . ' ' . lang('en'),
                    'rules' => 'required'
                )
            );
            $this->form_validation->set_rules($formVal);
            if ($this->form_validation->run() == TRUE) {
                if ($this->Status_m->update_status($id) > 0) {
                    $this->session->set_flashdata('error', lang('update') . ' ' . lang('success'));
                } else {
                    $this->session->set_flashdata('error', lang('update') . ' ' . lang('fail'));
                }
                redirect('admin_status');
            } else {
                $this->display_admin('admin/status/update_status');
            }
        } else {
            $this->display_admin('admin/status/update_status');
        }
    }

}
